home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Games / Game Sample Code / ZAM 1.0a13 / GameSource / InitGame.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-16  |  5.8 KB  |  295 lines  |  [TEXT/KAHL]

  1. /*
  2.     InitGame
  3.     
  4.     This big ole thing is what gets the game up and running.  It starts the timer
  5.     starts the sprites, loads all the graphics and sets all the flags just right.
  6.     
  7. */
  8.  
  9. /*  AppleEvents.h */
  10. /*     EPPC.h */
  11.  
  12. #include "CoreGlobals.h"
  13. #include "ZAM.h"
  14. #include "GameDef.h"
  15. #include "Document.h"
  16. #include "GameAEvents.h"
  17. #include "GameSounds.h"
  18. #include "ZAMProtos.h"
  19.  
  20.  
  21. gamePtr    gGame;
  22. Boolean gDead;
  23.  
  24. void MakeGameWindow(gamePtr game);
  25.  
  26. void InitGame(void)
  27. {
  28.  
  29.  
  30.     InitXThingTimer();
  31.     InitSprites();
  32.     InitDirectionTable();
  33.     gGame = MakeGameRecord();
  34.     
  35.     InstallCustomEvents(gGame);
  36.     MakeGameWindow(gGame);
  37.  
  38.     InitSounds("\pZAM Sounds");
  39.     (void)PlaySndAsynchChannel( kRiffSnd, kMusicChan, kStdPriority);
  40.     
  41.     LoadExplosionSprites(gGame);
  42.     LoadMissileSprites(gGame);
  43.     LoadTankSprites(gGame);
  44.  
  45.  
  46.     StartIncidentalSounds();
  47.  
  48.     gDead = false;
  49.     
  50. #ifdef NO_NET
  51.     // this will allow testing of the animation and sounds without needing another mac
  52.     game->gameState = kGameInProgress;
  53.     /* Set up tank indexes here */
  54.     game->localTankIndex = 0;
  55.     game->remoteTankIndex = 1;
  56.     PlaceTankSprites(game);
  57. #endif
  58.  
  59.     MainEvent();
  60.     MainEvent();
  61.     MainEvent();
  62.     MainEvent();
  63. }
  64.  
  65. OSErr PortInfoToAddressDesc(PortInfoRec *myPortInfo,
  66.                     EntityName *nbpEntity,
  67.                     AEAddressDesc *myTarget)
  68. {
  69.     TargetID    toTargetID;
  70.     OSErr        err;
  71.     
  72.     toTargetID.location.locationKindSelector = ppcNBPLocation;
  73.     toTargetID.location.u.nbpEntity = *nbpEntity;
  74.     toTargetID.name = myPortInfo->name;
  75.  
  76.     err = AECreateDesc(typeTargetID, 
  77.                 &toTargetID, sizeof(TargetID), myTarget);
  78.  
  79.     return err;
  80. }
  81.  
  82. OSErr GetTargetAddress(PortInfoRec *myPortInfo, 
  83.                         AEAddressDesc *myTarget,
  84.                         TargetID    *toTargetID)
  85. {
  86.     OSErr    err;
  87.     
  88.     err = PPCBrowser("\pSelect a ZAM game", "\pOpponents", false, &toTargetID->location,myPortInfo, nil,"\p");
  89.     if( (err != noErr) && (err != -128) ) {
  90.         ErrMsgCode("\pPPCBrowser failed. in GetTargetAddress",err);
  91.         return err;
  92.     }
  93.  
  94.     if(err == noErr) {    
  95.         err = PortInfoToAddressDesc(myPortInfo, &toTargetID->location.u.nbpEntity, myTarget);
  96.         if(err != noErr) {
  97.             ErrMsgCode("\pAECreateDesc failed.",err);
  98.         }
  99.     }
  100.  
  101.     return err;
  102. }
  103.  
  104. OSErr FindOpponent( AEAddressDesc *oppAddr)
  105. {
  106.  
  107.     OSErr                err;
  108.     TargetID            toTargetID;
  109.     PortInfoRec            oppPortInfo;
  110.     
  111.     err = GetTargetAddress(&oppPortInfo, 
  112.                         oppAddr,
  113.                         &toTargetID);
  114.                                      
  115.     if( (err != noErr) ) {
  116.         ErrMsgCode("\pPPCBrowser Failed.",err);
  117.     }
  118.     
  119.     return err;
  120.                      
  121. }
  122.  
  123. void MakeGameWindow(gamePtr game)
  124. {
  125.     
  126.     WindowPtr        wp;
  127.     wDispHandle        disp;
  128.     PicHandle        pict;
  129.     OSErr            err;
  130.     PixMapHandle    srcPix, destPix;
  131.     PaletteHandle    pal;
  132.     CTabHandle        cTab;
  133.     
  134.     wp = NewDispatchWindow(128);
  135.     if(!wp) {
  136.         ErrMsg("\pERROR CREATIUNG DISP WINDOW");
  137.         /* mem leak: not dumping ooppAddr */
  138.         return;
  139.     }
  140.     
  141.     pal = GetNewPalette(128);
  142.     if(!pal) {
  143.         ErrMsg("\pError getting the palette");
  144.     }
  145.     
  146.     cTab = (CTabHandle)NewHandle(256);        // this will be resized by Palette2CTab
  147.     Palette2CTab(pal,cTab);
  148.         
  149.     game->gameID = TickCount();
  150.     pict = GetPicture(128);
  151.  
  152.     if(pict == nil) {
  153.         ErrMsg("\pPict not loaded.  In HELL.");
  154.         ExitToShell();
  155.     }
  156.     err = CreateGWorldFromPictWithCTable(&game->backdrop, pict,cTab);
  157.     err = CreateGWorldFromPictWithCTable(&game->tween, pict, cTab);
  158.     ReleaseResource(pict);
  159.     
  160.     /* set the seeds of the gworlds to the same so copying is faster */
  161.     srcPix = GetGWorldPixMap(game->backdrop);
  162.     destPix = GetGWorldPixMap(game->tween);
  163.     
  164.     (**(**srcPix).pmTable).ctSeed = (**(**destPix).pmTable).ctSeed;
  165.     
  166.     /* now get the window pix map and set the seed */
  167.     srcPix = GetGWorldPixMap((GWorldPtr)wp);
  168.     
  169.     (**(**srcPix).pmTable).ctSeed = (**(**destPix).pmTable).ctSeed;
  170.  
  171.     game->gameCTSeed = (**(**srcPix).pmTable).ctSeed;
  172.     game->gameCTab = cTab;
  173.     game->gameWind = wp;
  174.     game->gameArea = wp->portRect;
  175.     
  176.     /* set up the game rectangles */
  177.     game->statusArea = wp->portRect;
  178.     game->statusArea.top = game->gameArea.bottom;
  179.     
  180.     SetWRefCon(wp, (long)game);
  181.     
  182.     /* this sets the window's CTSeed to the same one too, to further prevent color mapping */
  183.     
  184.     (**(**(*(CGrafPtr)wp).portPixMap).pmTable).ctSeed = game->gameCTSeed;
  185.     
  186.     ShowWindow(wp);
  187.  
  188.     srcPix = PreserveGraf(game->tween);
  189.     destPix = PreserveGraf((GWorldPtr)wp);
  190.     CopyBits(*srcPix,
  191.              *destPix,
  192.              &game->tween->portRect,
  193.              &wp->portRect,
  194.              srcCopy,
  195.              nil);        
  196.     ValidRect(&wp->portRect);
  197.     RestoreGraf();
  198.     RestoreGraf();
  199.  
  200.  
  201. }
  202.  
  203. gamePtr MakeGameRecord(void)
  204. {
  205.     gamePtr game;
  206.     
  207.     game = (gamePtr)NewPtrClear(sizeof(gameRec));
  208.     if(!game) {
  209.         ErrMsg("\pgame record not allocated.");
  210.     } else {
  211.         game->gameState = kWaitingForRequest;
  212.     }
  213.         
  214.     return game;
  215. }
  216.  
  217.  
  218. DisposeGameWindow(void)
  219. {
  220.     WindowPtr    wp;
  221.     gamePtr    game;
  222.     
  223.     wp = FrontWindow();
  224.     if(!wp) return;
  225.     
  226.     game = (gamePtr)GetWData(wp);
  227.     DisposeHandle(game);
  228.     DisposeDispatchWindow(wp);
  229. }
  230.  
  231.  
  232. OSErr NewGame(void)
  233. /*
  234.     This initiates the connection, and is called when the user selects
  235.     NEW from the file menu.
  236. */
  237. {
  238.     OSErr            err = noErr;
  239.     AEAddressDesc    oppAddr;
  240.     AppleEvent        reqEvent;
  241.     AppleEvent        reqReply;
  242.     long            replyResult;
  243.     long            actualType,longSize;
  244.     gamePtr            game;
  245.     WindowPtr        fWind;
  246.     GrafPtr            savePort;
  247.     
  248.     /* get the game handle */
  249.     fWind = FrontWindow();
  250.     if(fWind == nil) {
  251.         ErrMsg("\pNo Front Window and New Game occured.");
  252.         err = paramErr;
  253.     } else {
  254.         game = (gamePtr)GetWData(fWind);
  255.         if(game == nil) {
  256.             ErrMsg("\pWindow DATA is nil");
  257.             err = paramErr;
  258.         }
  259.     }
  260.  
  261.     if(err == noErr) {
  262.         err = FindOpponent( &oppAddr);
  263.         ForceRefreshGameWindow(fWind, game);
  264.     }
  265.         
  266.     if(err == noErr)    {
  267.  
  268.         game->oppAddr = oppAddr;
  269.         
  270.         err = AECreateAppleEvent(kZAMEventClass, kRequestGameID, &oppAddr,
  271.                      kAcceptID, game->gameID, &reqEvent);
  272.         if(err != noErr) {
  273.             ErrMsgCode("\p Failure: AECreateAppleEvent",err);
  274.         }
  275.                 
  276.         if(err == noErr) {
  277.             err = AESend(&reqEvent, &reqReply, kAEQueueReply + kAECanInteract,
  278.                      kAENormalPriority, 
  279.                      60 * 60, nil, nil);
  280.             if(err != noErr) {
  281.                 ErrMsgCode("\p Failure: AESend -- NewGame",err);
  282.             }
  283.         }
  284.         
  285.         if(err == noErr) {
  286.             game->gameState = kWaitingForAccept;
  287.         }
  288.         
  289.     }
  290.  
  291.     return err;
  292. }
  293.  
  294.  
  295.